package com.warrior.gatherweb.service.impl;

import com.warrior.gatherweb.service.GatherEnumService;
import com.warrior.gatherweb.vo.MetricVo;
import com.warrior.gatherweb.vo.PMetricVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName com.warrior.gatherweb.service.impl.GatherEnumServiceImpl.java
 * @Author xujian
 * @Description
 * @CreateTime 2021年01月04日 17:29:00
 */
@Service
@Slf4j
public class GatherEnumServiceImpl implements GatherEnumService {

    private static Map<String, PMetricVo> pMetricVos = new HashMap<>();

    static {
        PMetricVo cpuMetricVo = new PMetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR, "cpu相关");
        MetricVo cpuInfo = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_INFO, "cpu版本信息");
        MetricVo logicalProcessorCount = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_NUM, "cpu核心数");
        MetricVo systemUsed = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_SYSTEM_USE, "系统cpu使用率");
        MetricVo userUsed = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_USER_USE, "用户cpu使用率");
        MetricVo allUsed = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_ALL_USE, "cpu总使用率");
        MetricVo ioWait = new MetricVo(CommonStatus.CONSTANT_CENTRAL_PROCESSOR_ALL_USE, "cpu io等待率");
        cpuMetricVo.add(cpuInfo).add(logicalProcessorCount).add(systemUsed).add(userUsed).add(allUsed).add(ioWait);
        pMetricVos.put(CommonStatus.CONSTANT_CENTRAL_PROCESSOR, cpuMetricVo);

        PMetricVo networkif = new PMetricVo(CommonStatus.CONSTANT_NETWORK_IF, "网卡相关");
        MetricVo networkInfo = new MetricVo(CommonStatus.CONSTANT_NETWORK_IF_INFO, "网卡信息");
        MetricVo bytesSent = new MetricVo(CommonStatus.CONSTANT_NETWORK_IF_BYTESSENT, "网络上传流量");
        MetricVo bytesRecv = new MetricVo(CommonStatus.CONSTANT_NETWORK_IF_BYTESRECV, "网络下载流量");
        networkif.add(networkInfo).add(bytesSent).add(bytesRecv);
        pMetricVos.put(CommonStatus.CONSTANT_NETWORK_IF, networkif);

        PMetricVo operatingsystem = new PMetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM, "操作系统相关");
        MetricVo operatingVo = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_OPERATING, "操作系统版本");
        MetricVo processCount = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_PROCESS_COUNT, "进程数");
        MetricVo systemUptime = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_SYSTEMUPTIME, "系统运行时间(s)");
        MetricVo systemBootTime = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_BOOTTIME, "系统开机时间");
        MetricVo tcpv4Established = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_ESTABLISHED, "tcpv4 Established连接数");
        MetricVo tcpv4Active = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_ACTIVE, "tcpv4 Active连接数");
        MetricVo tcpv4Passive = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_PASSIVE, "tcpv4 Passive连接数");
        MetricVo tcpv4Failures = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_FAILURES, "tcpv4 Failures连接数");
        MetricVo tcpv4Reset = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_RESET, "tcpv4 Reset连接数");
        MetricVo tcpv4Sent = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_SENT, "tcpv4 Sent连接数");
        MetricVo tcpv4Received = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_RECEIVED, "tcpv4 Received连接数");
        MetricVo tcpv4Retransmitted = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_RETRANSMITTED, "tcpv4 Retransmitted连接数");
        MetricVo tcpv4Inerrors = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_INERRORS, "tcpv4 Inerrors连接数");
        MetricVo tcpv4Outresets = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV4_OUTRESETS, "tcpv4 Outresets连接数");
        MetricVo tcpv6Established = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_ESTABLISHED, "tcpv6 Established连接数");
        MetricVo tcpv6Active = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_ACTIVE, "tcpv6 Active连接数");
        MetricVo tcpv6Passive = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_PASSIVE, "tcpv6 Passive连接数");
        MetricVo tcpv6Failures = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_FAILURES, "tcpv6 Failures连接数");
        MetricVo tcpv6Reset = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_RESET, "tcpv6 Reset连接数");
        MetricVo tcpv6Sent = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_SENT, "tcpv6 Sent连接数");
        MetricVo tcpv6Received = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_RECEIVED, "tcpv6 Received连接数");
        MetricVo tcpv6Retransmitted = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_RETRANSMITTED, "tcpv6 Retransmitted连接数");
        MetricVo tcpv6Inerrors = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_INERRORS, "tcpv6 Inerrors连接数");
        MetricVo tcpv6Outresets = new MetricVo(CommonStatus.CONSTANT_OPERATIONG_SYSTEM_TCPV6_OUTRESETS, "tcpv6 Outresets连接数");
        operatingsystem.add(operatingVo).add(processCount).add(systemUptime).add(systemBootTime).add(tcpv4Established).add(tcpv4Active)
                .add(tcpv4Passive).add(tcpv4Failures).add(tcpv4Reset).add(tcpv4Sent).add(tcpv4Received).add(tcpv4Retransmitted).add(tcpv4Inerrors)
                .add(tcpv4Outresets).add(tcpv6Established).add(tcpv6Active).add(tcpv6Passive).add(tcpv6Failures).add(tcpv6Reset).add(tcpv6Sent)
                .add(tcpv6Received).add(tcpv6Retransmitted).add(tcpv6Inerrors).add(tcpv6Outresets);
        pMetricVos.put(CommonStatus.CONSTANT_OPERATIONG_SYSTEM, operatingsystem);

        PMetricVo osprocess = new PMetricVo(CommonStatus.CONSTANT_OSPROCESS, "OSProcess采集相关");
        MetricVo files = new MetricVo(CommonStatus.CONSTANT_OSPROCESS_FILES, "当前进程打开文件数");
        osprocess.add(files);
        pMetricVos.put(CommonStatus.CONSTANT_OSPROCESS, osprocess);

        PMetricVo filesystem = new PMetricVo(CommonStatus.CONSTANT_FILESYSTEM, "系统文件采集相关");
        MetricVo maxfile = new MetricVo(CommonStatus.CONSTANT_FILESYSTEM_FILES, "系统最大文件数");
        MetricVo totalSpace = new MetricVo(CommonStatus.CONSTANT_FILESYSTEM_TOTAL, "磁盘总空间");
        MetricVo usableSpace = new MetricVo(CommonStatus.CONSTANT_FILESYSTEM_FREE, "磁盘剩余空间");
        MetricVo usedSpace = new MetricVo(CommonStatus.CONSTANT_FILESYSTEM_USED, "磁盘已用空间");
        filesystem.add(maxfile).add(totalSpace).add(usableSpace).add(usedSpace);
        pMetricVos.put(CommonStatus.CONSTANT_FILESYSTEM, filesystem);

        PMetricVo globalmemory = new PMetricVo(CommonStatus.CONSTANT_GLOBALMEMORY, "系统内存采集相关");
        MetricVo total = new MetricVo(CommonStatus.CONSTANT_GLOBALMEMORY_TOTAL, "总内存");
        MetricVo available = new MetricVo(CommonStatus.CONSTANT_GLOBALMEMORY_AVAILABLE, "可用内存");
        MetricVo used = new MetricVo(CommonStatus.CONSTANT_GLOBALMEMORY_USED, "已用内存");
        globalmemory.add(total).add(available).add(used);
        pMetricVos.put(CommonStatus.CONSTANT_GLOBALMEMORY, globalmemory);

        PMetricVo gpu = new PMetricVo(CommonStatus.CONSTANT_GPU, "gpu采集相关");
        MetricVo utilization = new MetricVo(CommonStatus.CONSTANT_GPU_UTILIZATION, "显卡利用率");
        MetricVo name = new MetricVo(CommonStatus.CONSTANT_GPU_NAME, "显卡名称");
        MetricVo vram = new MetricVo(CommonStatus.CONSTANT_GPU_VRAM, "显卡总共内存");
        gpu.add(utilization).add(name).add(vram);
        pMetricVos.put(CommonStatus.CONSTANT_GPU, gpu);
    }

    @Override
    public Map<String, PMetricVo> getPMetricVos(String pName) {
        if (StringUtils.isEmpty(pName)) {
            return pMetricVos;
        }
        PMetricVo pMetricVo = pMetricVos.get(pName);
        Map<String, PMetricVo> result = new HashMap<>();
        result.put(pName, pMetricVo);
        return result;
    }
}
